package com.raising.modules.sys.utils;

import com.google.common.collect.Lists;
import com.raising.StaticParam;
import com.raising.framework.spring.SpringUtils;
import com.raising.modules.sys.dao.OrganizationDao;
import com.raising.modules.sys.entity.Organization;
import com.raising.utils.JStringUtils;
import com.raising.utils.UserUtils;

import java.util.List;

/**
 * 单位工具类
 * @author gaoy
 * @version 1.0
 */
public class OrgUtils {
    private static OrganizationDao organizationDao = SpringUtils.getBean(OrganizationDao.class);
    /**
     * @Author: chenwei
     * @Description: 获取权限内的军旅机构
     * @Date: 2018/2/27 16:46
     */
    public static List<Organization> getBeforeSecondOrganization(){
        String orgNo = UserUtils.getOrgNo();
        Organization organization = new Organization();
        organization.setStatus(StaticParam.VALID_STRING);
        organization.setAvailable(true);
        organization.setNo(orgNo);
        if(JStringUtils.isNotBlank(orgNo)){
            return organizationDao.getBeforeSecondOrganization(organization);
        }else{
            return Lists.newArrayList();
        }
    }
    /**
     * 获取所有的旅的机构
     * @author lirg
     * @TrainAssess 2018/3/9 15:01
     * @param 
     */
    public static List<Organization> getAllMilitaryBrigadeOrg(){
        return organizationDao.getAllMilitaryBrigadeOrg();
    }
    /**
     * 获取所有机构(真正的所有)
     * @author gaoy
     * @return
     */
    public static List<Organization> getAllList() {
        Organization organization = new Organization();
        organization.setStatus(StaticParam.VALID_STRING);
        organization.setAvailable(true);
        return organizationDao.getList(organization);
    }
    /**
     * 获取权限内所有机构
     * @author gaoy
     * @return
     */
    public static List<Organization> getMyOrgList() {
        String orgNo = UserUtils.getOrgNo();
        if(orgNo != null && !"".equals(orgNo) ){
            return organizationDao.getChildrenByNo(orgNo);
        }else{
            return Lists.newArrayList();
        }
    }
    /**
     * 根据id查询no
     * @author GaoYuan
     * @datetime 2018/1/24 下午8:16
     */
    public static String getNoById(String id){
        String no = organizationDao.getNoById(id);
        if(JStringUtils.isNotBlank(no) && no.startsWith(UserUtils.getOrgNo())){
            return no;
        }
        return null;
    }
    /**
     * 判断数据所在机构是否是权限范围内
     * @author gaoy
     * @return
     */
    public static boolean isMyOrg(String orgId) {
        if(JStringUtils.isBlank(orgId)){
            return false;
        }
        String no = organizationDao.getNoById(orgId);
        if(no != null && no.startsWith(UserUtils.getOrgNo())){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 判断数据所在机构是否是权限范围内
     * @author gaoy
     * @return
     */
    public static boolean isMyOrgByNo(String orgNo) {
        if(orgNo!=null && orgNo.startsWith(UserUtils.getOrgNo())){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取当前登录人所在机构标识
     * @author GaoYuan
     * @datetime 2018/1/29 下午9:40
     */
    public static String getMyOrgId() {
        try{
            return UserUtils.getOrgId();
        }catch (Exception e){
            return "--";
        }
    }
    /**
     * 获取当前登录人所在机构编号
     * @author GaoYuan
     * @datetime 2018/1/29 下午9:40
     */
    public static String getMyOrgNo() {
        try{
            return UserUtils.getOrgNo();
        }catch (Exception e){
            return "--";
        }
    }

}
